package com.gxa.saas.controller;


import com.gxa.saas.pojo.BrandFirst;
import com.gxa.saas.pojo.BrandSecond;
import com.gxa.saas.service.BrandSecondService;
import com.gxa.saas.service.BrandService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 二级品牌管理相关
 * @author CZJ
 * @date 2022/6/7 11:07
 */
@Api(value = "二级商品分类相关接口")
@RestController
@RequestMapping("/brandSecond")
@CrossOrigin
public class BrandSecondController {
    @Autowired
    BrandSecondService brandSecondService;

    @Autowired
    BrandService brandService;

    JsonResult jsonResult=new JsonResult();

    @ApiOperation(value = "新增二级品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "二级品牌的名称",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "from",value = "二级品牌对应父级品牌的信息",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "count",value = "二级品牌的count",dataType = "string",paramType = "query")
    })
    @PostMapping("/addBrandS")
    /**
     * 添加二级品牌接口
     */
    public Map addBrandSecond(String name,String from,String count){
        BrandSecond brandSecond = new BrandSecond();
        brandSecond.setName(name);
        brandSecond.setLevel("二级");
        brandSecond.setCount(count);
        BrandFirst brandFirst = brandService.selectByName(from);
        brandSecond.setFrom(brandFirst.getId());
        Integer insert = brandSecondService.insert(brandSecond);
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(insert);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "获取二级商品列表",produces = "query")
    @PostMapping("/select")
    /**
     * 获取二级商品列表
     */
    public Map getBrand(){
        List<BrandSecond> brandSeconds = brandSecondService.selectAll();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(brandSeconds);
        return jsonResult.getMap();
    }


    @ApiOperation(value = "分页查询二级商品接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页查询数",defaultValue = "10",dataType = "Integer",paramType = "query")
    })
    @PostMapping("/selectByPage")
    /**
     * 分页查询二级品牌信息
     */
    public Map getBrandByPage(@RequestParam(defaultValue = "1")  Integer page,@RequestParam(defaultValue = "10")  Integer size){
        List<BrandSecond> brandSeconds = brandSecondService.selectByPage(page, size);
        List<BrandSecond> brandSeconds1 = brandSecondService.selectAll();
        jsonResult.set("number",brandSeconds1.size());
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(brandSeconds);
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据id编辑二级品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandSecond",value = "修改的对象",dataType = "BrandSecond",paramType = "body")

    })
    @PostMapping("/updateBrand")
    /**
     * 修改品牌接口
     */
    public Map updateBrand(@RequestBody BrandSecond brandSecond){

        Integer update = brandSecondService.update(brandSecond);
        jsonResult.setData(update);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据id删除二级品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除的id",dataType = "Integer",paramType = "query")

    })
    @PostMapping("/deleteBrand")
    /**
     * 根据id删除
     */
    public Map deleteBrand(Integer id){
        Integer delete = brandSecondService.delete(id);
        jsonResult.setData(delete);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "一级品牌下的二级品牌查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "from",value = "查询的条件",dataType = "String",paramType = "query")

    })
    @PostMapping("/selectByFrom")
    /**
     * 一级品牌下的查询
     */
    public Map selectLike(String from){
        BrandFirst brandFirst = brandService.selectByName(from);
        List<BrandSecond> brandSeconds = brandSecondService.selectByFrom(brandFirst.getId());
        jsonResult.setData(brandSeconds);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


}
